package com.shanzmoo.h5.web;

import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.db.mall.entity.MallOrderDetailEntity;
import com.shanzmoo.db.mall.entity.MallOrderEntity;
import com.shanzmoo.h5.domain.dto.MallOrderDetailSaveDTO;
import com.shanzmoo.h5.domain.dto.MallOrderSaveDTO;
import com.shanzmoo.h5.domain.vo.MallOrderBriefVO;
import com.shanzmoo.h5.domain.vo.MallOrderVO;
import com.shanzmoo.h5.manager.WapMallOrderManager;
import com.shanzmoo.h5.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单服务
 *
 * @author Gzhao 2020/12/25
 */
@Api(tags = "订单服务")
@RestController
@RequestMapping("order")
public class MallOrderCenterController {

    @Autowired
    private WapMallOrderManager wapMallOrderManager;

    @ApiOperation(value = "新增订单")
    @PostMapping("/add/order")
    public SHttpResponse<String> addOrder(
            @RequestBody MallOrderSaveDTO saveDTO) {
        String memberId = String.valueOf(SecurityUtil.getCurrentMemeberId());
        MallOrderEntity mallOrderEntity = saveDTO.convert(memberId, SecurityUtil.getCurrentUsername());
        List<MallOrderDetailEntity> mallOrderDetailEntityList = MallOrderDetailSaveDTO.createList(saveDTO.getOrderDetailList());

        String orderNo = wapMallOrderManager.addOrder(mallOrderEntity, mallOrderDetailEntityList);

        return SHttpResponse.ok(orderNo);
    }

    @ApiOperation(value = "获取订单详细信息")
    @GetMapping("/info")
    public SHttpResponse<MallOrderVO> getInfo(@RequestParam Integer orderId) {
        Integer memberId = SecurityUtil.getCurrentMemeberId();
        return SHttpResponse.ok(wapMallOrderManager.getInfo(orderId, memberId));
    }

    @ApiOperation(value = "订单列表")
    @GetMapping("/list")
    public SHttpResponse<PageRespBase<MallOrderBriefVO>> queryList(
            @ApiParam(value = "当前页") @RequestParam(value = "pageNum") Integer pageNum,
            @ApiParam(value = "每页条数") @RequestParam(value = "pageSize") Integer pageSize,
            @ApiParam(value = "状态：0-待支付 1-待发货 2-已发货, 为空时查所有") @RequestParam(value = "status", required = false) Integer status) {
        String memberId = String.valueOf(SecurityUtil.getCurrentMemeberId());
        PageRespBase<MallOrderBriefVO> list = wapMallOrderManager.queryList(memberId, pageNum, pageSize, status);
        return SHttpResponse.ok(list);
    }

    @ApiOperation(value = "取消订单")
    @GetMapping("/cancel")
    public SHttpResponse<Boolean> cancel(@RequestParam Integer orderId) {
        Integer memberId = SecurityUtil.getCurrentMemeberId();
        SResponse<Boolean> re = wapMallOrderManager.cancel(orderId, memberId);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "买家确认收货")
    @GetMapping("/confirm/receipt")
    public SHttpResponse<Boolean> confirmReceipt(@RequestParam Integer orderId) {
        Integer memberId = SecurityUtil.getCurrentMemeberId();
        SResponse<Boolean> re = wapMallOrderManager.confirmReceipt(orderId, memberId);
        return SHttpResponse.transform(re);
    }


}
